package com.example.demo.controller;

import com.example.demo.commom.AjaxResult;
import com.example.demo.commom.JwtUtils;
import com.example.demo.model.dto.comment.OpCommentDto;
import com.example.demo.model.dto.comment.OpCommentListDto;
import com.example.demo.model.dto.comment.PCommentDto;
import com.example.demo.model.dto.comment.SCommentDto;
import com.example.demo.model.entity.PComment;
import com.example.demo.model.entity.SComment;
import com.example.demo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2024-05-29
 * Time:22:51
 */
@RestController
@RequestMapping("/album/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    // 发布父评论
    @PostMapping
    public AjaxResult pubPComment(HttpServletRequest request,@RequestBody PCommentDto dto){
        // 参数检查
        if(dto == null || dto.getTarget_id() == null || dto.getTarget_id() <= 0 || dto.getContent() == null) {
            return AjaxResult.fail(1000);
        }
        Integer userId = JwtUtils.getUserIdByRequest(request);
        Integer res = commentService.pubPComment(userId, dto);
        if(res == null || res != 1){
            return AjaxResult.fail("评论失败，请稍后再试~");
        }
        return AjaxResult.success("评论成功");
    }
    // 发布子评论
    @PostMapping("/child")
    public AjaxResult pubSComment(HttpServletRequest request,@RequestBody SCommentDto dto){
        // 参数检查
        if(dto == null || dto.getTarget_id() == null || dto.getTarget_id() <= 0 || dto.getParent_id() == null || dto.getParent_id() <= 0 || dto.getContent() == null){
            return AjaxResult.fail(1000);
        }
        Integer userId = JwtUtils.getUserIdByRequest(request);
        Integer res = commentService.pubSComment(userId, dto);
        if(res == null || res != 1){
            return AjaxResult.fail("评论失败，请稍后再试~");
        }
        return AjaxResult.success("评论成功");
    }
    // 父评论列表
    @GetMapping("/list")
    public AjaxResult listPComment(@RequestParam(required = false) Integer start,
                                   @RequestParam(required = false) Integer limit,
                                   @RequestParam() Integer id){
        // 参数校验
        if(id == null || id <= 0){
            return AjaxResult.fail(1000);
        }
        if(start == null || start < 0){
            start = 0;
        }
        if(limit == null || limit < 5){
            limit = 5;
        }
        List<PComment> res =  commentService.listPComment(start, limit, id);
        Map<String, Object> map = new HashMap<>();
        map.put("res", res);
        map.put("more", false);
        return AjaxResult.success(String.valueOf(map));
    }
    // 子评论列表
    @GetMapping("/child_list")
    public AjaxResult listSComment(@RequestParam(required = false) Integer start,
                                   @RequestParam(required = false) Integer limit,
                                   @RequestParam() Integer id){
        // 参数校验
        if(id == null || id <= 0){
            return AjaxResult.fail(1000);
        }
        if(start == null || start < 0){
            start = 0;
        }
        if(limit == null || limit < 5){
            limit = 5;
        }
        List<SComment> res =  commentService.listSComment(start, limit, id);
        Map<String, Object> map = new HashMap<>();
        map.put("res", res);
        map.put("more", false);
        return AjaxResult.success(String.valueOf(map));
    }
    // 删除父评论列表
    @PostMapping("/del")
    public AjaxResult delPComment(HttpServletRequest request,@RequestBody OpCommentDto dto){
        // 参数校验
        if(dto.getId() == null || dto.getId() <= 0){
            return AjaxResult.fail(1000);
        }
        Integer userId = JwtUtils.getUserIdByRequest(request);
        Integer res =  commentService.delPComment(userId, dto);
        if(res == -1){
            return  AjaxResult.fail(1003);
        } else if(res == 1){
            // 删除成功后，需要把父下的所有子评论删除掉
            commentService.delSCommentList(dto);
            return AjaxResult.success("ok");
        } else if(res == -2){
            return AjaxResult.fail(1004);
        } else{
            return AjaxResult.fail(1004);
        }
    }
    // 删除子评论
    @PostMapping("/del/child")
    public AjaxResult delSComment(HttpServletRequest request,@RequestBody OpCommentDto dto){
        // 参数校验
        if(dto.getId() == null || dto.getId() <= 0){
            return AjaxResult.fail(1000);
        }
        Integer userId = JwtUtils.getUserIdByRequest(request);
        Integer res =  commentService.delSComment(userId, dto);
        if(res == -1){
            return  AjaxResult.fail(1003);
        } else if(res == 1){
            return AjaxResult.success("ok");
        } else if(res == -2){
            return AjaxResult.fail(1004);
        } else{
            return AjaxResult.fail(1004);
        }
    }
    // 点赞父评论
    @PostMapping("/like")
    public AjaxResult collectPComment(HttpServletRequest request,@RequestBody OpCommentDto dto){
        return null;
    }
    // 点赞子评论
    @PostMapping("/child/like")
    public AjaxResult collectSComment(HttpServletRequest request,@RequestBody OpCommentDto dto){
        return null;
    }
}
